package com.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dao.DeptDao;
import com.dao.DeptDaoImpl;
import com.entity.Dept;

@Controller
@RequestMapping("/dept")
public class DeptController {

	private DeptDao deptDao = new DeptDaoImpl();
	
	
	@RequestMapping("/update")
	public ModelAndView update(Dept dept){
		int result = deptDao.updateDept(dept);
		System.out.println("更新结果:"+result);
		return findAll();
	}
	
	@RequestMapping("/toupdate/{id}")
	public String toupdate(@PathVariable("id") Integer id,Map<String,Dept> map){
		Dept dept = deptDao.findById(id);
		map.put("d", dept);
		return "dept_update";
	}
	
	@RequestMapping("/save")
	public ModelAndView save(Dept dept){
		int result = (Integer) deptDao.saveDept(dept);
		return findAll();
	}
	
	
	@RequestMapping("/delete/{id}")
	public ModelAndView delete(@PathVariable("id") Integer id){
		int result = deptDao.deleteDept(id);		
		return findAll();
	}
	
	@RequestMapping("/findAll")
	public ModelAndView findAll(){
		ModelAndView mav = new ModelAndView("dept_list");
		List<Dept> list = deptDao.findAll();
		mav.addObject("list", list);
		return mav;
	}
}
